#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   SessionControl_0x10_test.py
@Time    :   2022/08/23 13:31:28
@Author  :   YangLiang 
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
# from lib2to3.pgen2 import driver
from pytest_assume.plugin import assume

import pytest
import logging
import time
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

sessionControl_noResp_extend_func = [([0x10, 0x04], []),
                                     ([0x10, 0x05], []),
                                     ([0x10, 0x06], []),
                                     ([0x23], []),
                                     ([0x2A], []),
                                     ([0x2C], []),
                                     # According to project def：功能寻址是否支持0x10,0x02，功能寻址支持注释，否则不注释
                                     ([0x10, 0x02], [])
                                     ]

boot_sessionControl_NRC7E = [([0x10, 0x03], [0x7F, 0x10, 0x7E]),
                             ([0x10, 0x83], [0x7F, 0x10, 0x7E]),
                             # ([0x10,0x02],[0x7F,0x10,0x7E]),  # According to project def：boot下是否支持0x10,0x02，支持注释，否则不注释
                             # ([0x10,0x82],[0x7F,0x10,0x7E])  # According to project def：boot下是否支持0x10,0x02，支持注释，否则不注释
                             ]

boot_SessionControl_PosMsg = [([0x10, 0x01], [0x50, 0x01, 0x00, 0x32, 0x01, 0xF4]),
                              ([0x10, 0x02], [0x7F, 0x10,0x33])]  # According to project def：boot下是否支持0x10,0x02，支持不注释，否则注释

sessionControl = [  ([0x10, 0x01], [0x50, 0x01, 0x00, 0x32, 0x01, 0xF4]),
                    ([0x10, 0x03], [0x50, 0x03, 0x00, 0x32, 0x01, 0xF4]),
                    ([0x10, 0x02], [0x7F, 0x10, 0x33])
                ]


sessionControl_Suppress_default = [([0x10, 0x81], []),
                                   ([0x10, 0x83], [])]

sessionControl_Suppress_extend = [([0x10, 0x83], []),
                                  ([0x10, 0x81], [])]

sessionControl_NRC_phy = [([0x10], [0x7F, 0x10, 0x13]),
                          ([0x10, 0x01, 0xFF], [0x7F, 0x10, 0x13]),
                          ([0x10, 0x03, 0x10], [0x7F, 0x10, 0x13]),
                          ([0x10, 0x05, 0x06], [0x7F, 0x10, 0x13]),
                          ([0x10, 0x04], [0x7F, 0x10, 0x12]),
                          ([0x10, 0x05], [0x7F, 0x10, 0x12]),
                          ([0x10, 0x06], [0x7F, 0x10, 0x12]),
                          ([0x23], [0x7F, 0x23, 0x11]),
                          ([0x2A], [0x7F, 0x2A, 0x11]),
                          ([0x2C], [0x7F, 0x2C, 0x11]),
                          ([0x2F], [0x7F, 0x2F, 0x11]),
                          ]

sessionControl_SuppressPosMsg_NRC_phy = [
    ([0x10, 0x81, 0xFF], [0x7F, 0x10, 0x13]),
    ([0x10, 0x83, 0x10], [0x7F, 0x10, 0x13]),
    ([0x10, 0x84], [0x7F, 0x10, 0x12]),
    ([0x10, 0x85], [0x7F, 0x10, 0x12]),
    ([0x10, 0x86], [0x7F, 0x10, 0x12])]

sessionControl_NRC7E_phy = [([0x10, 0x02], [0x7F, 0x10, 0x7E])]

# 20220913 Revise:BYD 企标规定 功能寻址 NRC=11 12 13 7E 7F 31都不响应
sessionControl_NRC13_func = [([0x10], []),              #[0x7F, 0x10, 0x13]),
                             ([0x10, 0x01, 0xFF], []),  #[0x7F, 0x10, 0x13]),
                             ([0x10, 0x03, 0x10], [])]  #[0x7F, 0x10, 0x13])]

sessionControl_noResp_default_func = [([0x10, 0x04], []),
                                      ([0x10, 0x05], []),
                                      ([0x10, 0x06], []),
                                      ([0x10, 0x02], []),
                                      ([0x27, 0x03], []),
                                      ([0x2E, 0x3F, 0x01], []),
                                      ([0x28, 0x00, 0x01], []),
                                      ([0x85, 0x01], []),
                                      ([0x23], []),
                                      ([0x2A], []),
                                      ([0x2C], [])]


sessionControl_suppressPosMsgBit_NRC13 = [([0x10, 0x81, 0xFF], [0x7F, 0x10, 0x13]),
                                          ([0x10, 0x83, 0x10], [0x7F, 0x10, 0x13])]

sessionControl_noResp_suppressPosMsgBit_default_func = [([0x10, 0x84], []),
                                                        ([0x10, 0x85], []),
                                                        ([0x10, 0x86], []),
                                                        ([0x10, 0x82], [])]

sessionControl_noResp_suppressPosMsgBit_extend_func = [([0x10, 0x84], []),
                                                       ([0x10, 0x85], []),
                                                       ([0x10, 0x86], [])]

boot_sessionControl_NRC = [([0x10], [0x7F, 0x10, 0x13]),
                           ([0x10, 0x02, 0xFF], [0x7F, 0x10, 0x13]),
                           ([0x10, 0x02, 0x10], [0x7F, 0x10, 0x13]),
                           ([0x10, 0x04], [0x7F, 0x10, 0x12]),
                           ([0x10, 0x05], [0x7F, 0x10, 0x12]),
                           ([0x10, 0x06], [0x7F, 0x10, 0x12])]

boot_sessionControl_suppressPosMsgBit_NRC = [([0x10, 0x82, 0xFF], [0x7F, 0x10, 0x13]),
                                             ([0x10, 0x82, 0x10], [0x7F, 0x10, 0x13]),
                                             ([0x10, 0x84], [0x7F, 0x10, 0x12]),
                                             ([0x10, 0x85], [0x7F, 0x10, 0x12]),
                                             ([0x10, 0x86], [0x7F, 0x10, 0x12])]

sessionControl_default_str = [([0x10, 0x01], [0x50, 0x01, 0x00, 0x32, 0x01, 0xF4]),
                              ([0x10, 0x03], [0x50, 0x03, 0x00, 0x32, 0x01, 0xF4])]


class Test_SessionControl_Phy_App(object):

    @allure.severity('normal')
    @allure.story('Session Control Test')
    @allure.title('Test repeat five count session change [1001->1003] with with physical addressing for 0x10')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @allure.description('Test session change session in 1001->1003')
    def test_sessionSwitch1_Phy(self, action, Driver):
        iStep = 1
        for i in range(5):
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x01], [0x50, 0x01, 0x00, 0x32, 0x01, 0xF4], 0, iStep, False)
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x03], [0x50, 0x03, 0x00, 0x32, 0x01, 0xF4], 0, iStep, False)
  

    @allure.severity('normal')
    @allure.title('Test session change [1001->1003->1101] with with physical addressing for 0x10')
    @allure.description('Test session change from 1001->1003->1101')
    def test_sessionSwitch2_Phy(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x01], [0x50, 0x01, 0x00, 0x32, 0x01, 0xF4], 0, iStep, False)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x03], [0x50, 0x03, 0x00, 0x32, 0x01, 0xF4], 0, iStep, False)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x11, 0x01], [0x7F, 0x11,0X33], 0, iStep, False)


    @allure.severity('normal')
    @allure.title('Test session change [1001->1003->1103->1101] with with physical addressing for 0x10')
    @allure.description('Test session change from 1001->1003->1103->1101')
    def test_sessionSwitch3_Phy(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x01], [0x50, 0x01, 0x00, 0x32, 0x01, 0xF4], 0, iStep, False)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x03], [0x50, 0x03, 0x00, 0x32, 0x01, 0xF4], 0, iStep, False)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x11, 0x03], [0x7F, 0x11,0X33], 0, iStep, False)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x11, 0x01], [0x51, 0x01], 0, iStep, True)


    @allure.severity('normal')
    @allure.title('Test session change [1001->1003->1002->restart->22F186] with with physical addressing for 0x10')
    @allure.description('Test session change from 1001->1003->1101')
    def test_sessionSwitch4_Phy(self, action, Driver):
        iStep = 1
        for i in range(1):
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x03], [0x50, 0x03, 0x00, 0x32, 0x01, 0xF4], 0, iStep, False)
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x02], [0x50, 0x02, 0x00, 0x32, 0x01, 0xF4], 0, iStep, True)
            time.sleep(0.5)
            iStep = Driver['CAN'].restartMRR_Power(Driver,iStep)
            with allure.step(f'Step{iStep}:wait 3s'):
                time.sleep(4)
                iStep += 1
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x22, 0xF1,0X86], [0x62, 0xF1, 0x86, 0x01], 0, iStep, False)




    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in sessionControl]
    @allure.severity('normal')
    @allure.title('Test {req} in default session with with physical addressing for 0x10')
    @allure.description('Test positive response of SID 0x10 in default session with physical addressing')
    @pytest.mark.parametrize('req,resp', sessionControl, ids=ids)
    def test_sessionControl_Default_Phy(self, action,  Driver, req, resp):
        iStep = 1
        if resp == [0x7F,0x10, 0x33]:
            resp = [0x7F,0x10, 0x7E]
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False)

    
    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in sessionControl]
    @allure.severity('normal')
    @allure.title('Test {req} in extended session with with physical addressing for 0x10')
    @allure.description('Test positive response of SID 0x10 in extended session with physical addressing')
    @pytest.mark.parametrize('req,resp', sessionControl, ids=ids)
    def test_sessionControl_Extended_Phy(self, action,  Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)


    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in sessionControl_NRC_phy]
    @allure.severity('minor')
    @allure.title('Test {req} of NRC in default session with with physical addressing for 0x10')
    @allure.description('Test NRC of SID 0x10 in default sesison with physical addressing')
    @pytest.mark.parametrize('req,resp', sessionControl_NRC_phy, ids=ids)
    def test_sessionControl_NRC_Default_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False)


    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in sessionControl_NRC7E_phy]
    @allure.severity('minor')
    @allure.title('Test {req} of NRC7E in default session with with physical addressing for 0x10')
    @allure.description('Test NRC7E of SID 0x10 when suppressPosMsgBit is enabled in default session with physical addressing')
    @pytest.mark.parametrize('req,resp', sessionControl_NRC7E_phy, ids=ids)
    def test_sessionControl_NRC7E_Default_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False)


    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in sessionControl_NRC_phy]
    @allure.severity('minor')
    @allure.title('Test {req} of NRC in extended session with with physical addressing for 0x10')
    @allure.description('Test NRC of SID 0x10 in extended sesison with physical addressing')
    @pytest.mark.parametrize('req,resp', sessionControl_NRC_phy, ids=ids)
    def test_sessionControl_NRC_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)

    
    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in sessionControl_Suppress_default]
    @allure.severity('normal')
    @allure.description('Test positive response of 0x10 when suppressPosMsgBit is enabled in default session with physical addressing')
    @pytest.mark.parametrize('req,resp', sessionControl_Suppress_default, ids=ids)
    def test_sessionControl_SuppressPosMsg_Default_Phy(self, action,  Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False)



    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in sessionControl_Suppress_extend]
    @allure.severity('normal')
    @allure.description('Test positive response of 0x10 when suppressPosMsgBit is enabled in extended session with physical addressing')
    @pytest.mark.parametrize('req,resp', sessionControl_Suppress_extend, ids=ids)
    def test_sessionControl_SuppressPosMsg_Extended_Phy(self, action,  Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)

    
    
    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in sessionControl_SuppressPosMsg_NRC_phy]
    @allure.severity('minor')
    @allure.description('Test NRC of 0x10 when suppressPosMsgBit is enabled in default session with physical addressing')
    @pytest.mark.parametrize('req,resp', sessionControl_SuppressPosMsg_NRC_phy, ids=ids)
    def test_sessionControl_NRC_SuppressPosMsg_Default_Phy(self, action,  Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False)



    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in sessionControl_SuppressPosMsg_NRC_phy]
    @allure.severity('minor')
    @allure.description('Test NRC of 0x10 when suppressPosMsgBit is enabled in extended session with physical addressing')
    @pytest.mark.parametrize('req,resp', sessionControl_SuppressPosMsg_NRC_phy, ids=ids)
    def test_sessionControl_NRC_SuppressPosMsg_Extended_Phy(self, action,  Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)



    @allure.severity('minor')
    @allure.description('Test Test NRC Priority of 0x10 in extended session with physical addressing')
    def test_sessionControl_NRC_Priority_Extended_Phy(self, action, Driver):
        iStep = 1
        # 设置电压为17v，并等待1.5s
        iStep = Driver['CAN'].SetVolt(Driver, 17, 1.5, iStep)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x02, 0x00], [0x7F, 0x10, 0x13], 3, iStep, False)
        # 设置电压为12v，并等待1.5s
        iStep = Driver['CAN'].SetVolt(Driver, 12, 1.5, iStep)


    @allure.severity('minor')
    @allure.description('Test S_CAN of 0x10 with physical addressing')
    def test_sessionControl_PCAN_Default_Phy(self, action, Driver):
        Driver['CAN'].fush_q_buff()
        with allure.step('Step1:向私CAN发送请求10 01'):
            uds_rep, size = Driver['UDS2'].req_and_res_can([0x10,0x01], 2)
            respData,respSize = Driver['CAN'].read_PCAN_Frame()
            with assume: assert size == 0 and respSize == 0


class Test_SessionControl_Func_App(object):
    '''
    test SessionControl with Functional address in APP mode
    '''
    @allure.severity('normal')
    @allure.description('Test session change from 0x10 01->0x10 03->0x10 01')
    def test_sessionSwitch1_Func(self, action, Driver):
        
        iStep = 1
        for i in range(5):
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x01], [0x50, 0x01, 0x00, 0x32, 0x01, 0xF4], 0, iStep, False, 'UDSFun', 'noClearFault')
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x03], [0x50, 0x03, 0x00, 0x32, 0x01, 0xF4], 0, iStep, False, 'UDSFun', 'noClearFault')

    @allure.severity('normal')
    @allure.description('Test session change from 0x10 01->0x10 03->0x11 01')
    def test_sessionSwitch2_Func(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x01], [0x50, 0x01, 0x00, 0x32, 0x01, 0xF4], 0, iStep, False, 'UDSFun', 'noClearFault')
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x03], [0x50, 0x03, 0x00, 0x32, 0x01, 0xF4], 0, iStep, False, 'UDSFun', 'noClearFault')
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x11, 0x01], [], 0, iStep, False, 'UDSFun', 'noClearFault')
        

    @allure.severity('normal')
    @allure.description('Test session change from 0x10 01->0x10 03->0x11 03')
    def test_sessionSwitch3_Func(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x01], [0x50, 0x01, 0x00, 0x32, 0x01, 0xF4], 0, iStep, False, 'UDSFun', 'noClearFault')
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x03], [0x50, 0x03, 0x00, 0x32, 0x01, 0xF4], 0, iStep, False, 'UDSFun', 'noClearFault')
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x11, 0x03], [], 0, iStep, False, 'UDSFun', 'noClearFault')


    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in sessionControl]
    @allure.severity('normal')
    @allure.description('Test positive response of SID 0x10 in default session with functional addressing')
    @pytest.mark.parametrize('req,resp', sessionControl, ids=ids)
    def test_sessionControl_Default_Func(self, action, Driver, req, resp):
        iStep = 1
        if resp == [0x7F, 0x10, 0x33]:
            resp = []
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDSFun', 'noClearFault')



    ids1 = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in sessionControl]
    @allure.severity('normal')
    @allure.description('Test positive response of SID 0x10 in extended session with functional addressing')
    @pytest.mark.parametrize('req,resp', sessionControl, ids=ids1)
    def test_sessionControl_Extended_Func(self,action,Driver, req, resp):
        iStep = 1
        if resp == [0x7F, 0x10, 0x33]:
            resp = []
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun', 'noClearFault')



    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in sessionControl_NRC13_func]
    @allure.severity('minor')
    @allure.description('Test NRC of 0x10 in default session with functional addressing')
    @pytest.mark.parametrize('req,resp', sessionControl_NRC13_func, ids=ids)
    def test_sessionControl_NRC13_Default_Func(self, action,  Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDSFun', 'noClearFault')



    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in sessionControl_noResp_default_func]
    @allure.severity('minor')
    @allure.description('Test no response of SID 0x10  in default session with funcational addressing')
    @pytest.mark.parametrize('req,resp', sessionControl_noResp_default_func, ids=ids)
    def test_sessionControl_No_Resp_Default_Func(self, action,  Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDSFun', 'noClearFault')



    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in sessionControl_NRC13_func]
    @allure.severity('minor')
    @allure.description('Test NRC of 0x10 in extended session with functional addressing')
    @pytest.mark.parametrize('req,resp', sessionControl_NRC13_func, ids=ids)
    def test_sessionControl_NRC13_Extended_Func(self,  Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun', 'noClearFault')


    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in sessionControl_noResp_extend_func]
    @allure.severity('minor')
    @allure.description('Test no response of SID 0x10  in default session with funcational addressing')
    @pytest.mark.parametrize('req,resp', sessionControl_noResp_extend_func, ids=ids)
    def test_sessionControl_No_Resp_Extended_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun', 'noClearFault')

    
    
    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in sessionControl_Suppress_default]
    @allure.severity('normal')
    @allure.description('Test positive response of 0x10 when suppressPosMsgBit is enabled in default session with functional addressing')
    @pytest.mark.parametrize('req,resp', sessionControl_Suppress_default, ids=ids)
    def test_sessionControl_SuppressPosMsg_Default_Func(self, action,  Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDSFun', 'noClearFault')

    
    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in sessionControl_Suppress_extend]
    @allure.severity('normal')
    @allure.description('Test positive response of 0x10 when suppressPosMsgBit is enabled in extended session with functional addressing')
    @pytest.mark.parametrize('req,resp', sessionControl_Suppress_extend, ids=ids)
    def test_sessionControl_SuppressPosMsg_Extended_Func(self, action,  Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun', 'noClearFault')

    
    
    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in sessionControl_suppressPosMsgBit_NRC13]
    @allure.severity('minor')
    @allure.description('Test NRC13 of 0x10 when suppressPosMsgBit is enabled in default session with functional addressing')
    @pytest.mark.parametrize('req,resp', sessionControl_suppressPosMsgBit_NRC13, ids=ids)
    def test_sessionControl_NRC13_uppressPosMsg_Default_Func(self, action,  Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, [], 1, iStep, False, 'UDSFun', 'noClearFault')
                

    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in sessionControl_noResp_suppressPosMsgBit_default_func]
    @allure.severity('minor')
    @pytest.mark.parametrize('req,resp', sessionControl_noResp_suppressPosMsgBit_default_func, ids=ids)
    def test_sessionControl_No_Resp_SuppressPosMsg_Default_Func(self, action,  Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDSFun', 'noClearFault')

    
    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in sessionControl_suppressPosMsgBit_NRC13]
    @allure.severity('minor')
    @allure.description('Test NRC13 of 0x10 when suppressPosMsgBit is enabled in default session with functional addressing')
    @pytest.mark.parametrize('req,resp', sessionControl_suppressPosMsgBit_NRC13, ids=ids)
    def test_sessionControl_NRC13_uppressPosMsg_Extended_Func(self, action,  Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, [], 3, iStep, False, 'UDSFun', 'noClearFault')

    
    
    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in sessionControl_noResp_suppressPosMsgBit_extend_func]
    @allure.severity('minor')
    @allure.description('Test no response of 0x10 when suppressPosMsgBit is enabled in extended session with functional addressing')
    @pytest.mark.parametrize('req,resp', sessionControl_noResp_suppressPosMsgBit_extend_func, ids=ids)
    def test_sessionControl_No_Resp_SuppressPosMsg_Extended_Func(self, action,  Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun', 'noClearFault')

    @allure.severity('minor')
    @allure.description('Test Test NRC Priority of 0x10 in extended session with function addressing')
    def test_sessionControl_NRC_Priority_Extended_Func(self, action, Driver):
        iStep = 1
        # 设置电压为17v，并等待1.5s
        iStep = Driver['CAN'].SetVolt(Driver, 17, 1.5, iStep)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10,0x02], [], 3, iStep, False, 'UDSFun', 'noClearFault')
        # 设置电压为12v，并等待1.5s
        iStep = Driver['CAN'].SetVolt(Driver, 12, 1.5, iStep)


class Test_SessionControl_Phy_Boot(object):
    '''
    test SessionControl with physical address in programming mode
    '''
    @allure.severity('normal')
    @allure.story('Session Control Test in BL')
    @allure.description('Test session change from 0x10 01->0x10 03->0x10 02->0x10 02->0x10 01')
    def test_sessionSwitch1_Phy(self, action, Driver):
        iStep = 1
        iStep = Driver['CAN'].restartMRR_Power(Driver,iStep)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x02], [0x50, 0x02, 0x00, 0x32, 0x01, 0xF4], 3, iStep, True)
        time.sleep(1)
        iStep = Driver['CAN'].unlock(Driver, iStep)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x02], [0x50, 0x02, 0x00, 0x32, 0x01, 0xF4], 0, iStep, False)
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x01], [0x50, 0x01, 0x00, 0x32, 0x01, 0xF4], 0, iStep, False)
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27, 0x01], [0x7F, 0x27, 0x7F], 0, iStep, False)


    @allure.severity('normal')
    @allure.description('Test session change from 0x10 01->0x10 03->0x10 02->0x11 01')
    def test_sessionSwitch2_Phy(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x02], [0x50, 0x02, 0x00, 0x32, 0x01, 0xF4], 3, iStep, True)
        time.sleep(1)
        iStep = Driver['CAN'].unlock(Driver, iStep)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x11, 0x01], [0x51, 0x01], 0, iStep, False)
        time.sleep(3)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27, 0x01], [0x7F, 0x27, 0x7F], 0, iStep, False)

    @allure.severity('normal')
    @allure.story('Session Control Test in BL')
    @allure.description('Test session change from 0x10 01->0x10 03->0x10 02->0x10 02->0x10 01')
    def test_sessionSwitch4_Phy(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x02], [0x50, 0x02, 0x00, 0x32, 0x01, 0xF4], 3, iStep, True)
        time.sleep(6)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x03], [0x50, 0x03, 0x00, 0x32, 0x01, 0xF4], 0, iStep, False)



    @allure.severity('normal')
    @allure.description('Test session change from 0x10 01->0x10 03->0x10 02->0x11 03')
    def test_sessionSwitch3_Phy(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x02], [0x50, 0x02, 0x00, 0x32, 0x01, 0xF4], 3, iStep, True)
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x11, 0x01], [0x7F, 0x11, 0x33], 0, iStep, False)
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27, 0x02, 0x00, 0x01, 0x02, 0x03], [0x7F, 0x27, 0x24], 0, iStep, False)


    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in boot_SessionControl_PosMsg]
    @allure.severity('normal')
    @allure.description("Test positive response of SID 0x10 in bootloader")
    @pytest.mark.parametrize('req,resp', boot_SessionControl_PosMsg, ids=ids)
    def test_sessionControl_PosMsg_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False)


    @allure.severity('normal')
    @allure.description("Test positive response of SID 0x10 in bootloader")
    @pytest.mark.parametrize('req,resp', boot_SessionControl_PosMsg, ids=ids)
    def test_sessionControl_PosMsg_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)

    
    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in boot_sessionControl_NRC]
    @allure.severity('minor')
    @allure.story('Test NRC of SID 0x10 in bootloader with physical addressing')
    @pytest.mark.parametrize('req,resp', boot_sessionControl_NRC, ids=ids)
    def test_sessionControl_NRC_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False)



    @allure.severity('normal')
    @allure.title("Test positive response of 0x10 when suppressPosMsgBit is enabled in programming session with physical addressing")
    def test_sessionControl_SuppressPosMsg_Phy(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x82], [0x7F, 0x10, 0x12], 2, iStep, False)
        time.sleep(1)
        iStep = Driver['CAN'].unlock(Driver, iStep)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x82], [0x7F, 0x10, 0x12], 0, iStep, False)



    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in boot_sessionControl_suppressPosMsgBit_NRC]
    @allure.severity('minor')
    @allure.description("Test NRC of 0x10 when suppressPosMsgBit is enabled in bootloader")
    @pytest.mark.parametrize('req,resp', boot_sessionControl_suppressPosMsgBit_NRC, ids=ids)
    def test_sessionControl_NRC_SuppressPosMsg_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False)



    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in boot_sessionControl_NRC7E]
    @allure.severity('minor')
    @allure.story('Test NRC7E of SID 0x10 in bootloader with physical addressing')
    @pytest.mark.parametrize('req,resp', boot_sessionControl_NRC7E, ids=ids)
    def test_sessionControl_NRC7E_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False)